﻿// -----------------------------------------------------------------------
// <copyright file="RSASignature.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Encryption
{
    using System;
    using System.IO;
    using System.Text;
    using System.Security.Cryptography;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class RSASignature
    {
        #region Constant

        private const int SIGN_SIGNATURE = 1;
        private const int VERIFY_SIGNATURE = 0;
        private const string MD5_HASH_METHOD = "MD5";
        private const string SHA1_HASH_METHOD = "SHA1";

        #endregion

        #region Variable

        private string inputDocPath;
        private string outputDocPath;
        private string keyContent;

        #endregion

        #region Constructor

        public RSASignature()
        {
            inputDocPath = string.Empty;
            outputDocPath = string.Empty;
            keyContent = string.Empty;
        }

        public RSASignature( string fileIn, string fileOut, string key )
        {
            inputDocPath = fileIn;
            outputDocPath = fileOut;
            keyContent = key;
        }

        #endregion

        #region Method

        #region Sign

        public bool Sign(string docFilePath, string CipherFilePath, string privateKeyFilePath )
        {
            bool bResult = false;

            string text = "Hello world!";
            byte[] data = Encoding.ASCII.GetBytes( text );
            using ( RSACryptoServiceProvider rsa = new RSACryptoServiceProvider() )
            {
                byte[] signature = rsa.SignData( data, "SHA256" );
                System.IO.File.WriteAllText( @"D:\test\signature.txt", Convert.ToBase64String( signature ) );

                byte[] privateKey = rsa.ExportCspBlob( true );
                System.IO.File.WriteAllText( @"D:\test\privateKey.txt", Convert.ToBase64String( privateKey ) );

                byte[] publicKey = rsa.ExportCspBlob( false );
                System.IO.File.WriteAllText( @"D:\test\publicKey.txt", Convert.ToBase64String( publicKey ) );

                if ( rsa.VerifyData( data, "SHA256", signature ) )
                {
                    Console.WriteLine( "RSA-SHA256 signature verified" );
                }
                else
                {
                    Console.WriteLine( "RSA-SHA256 signature failed to verify" );
                }
            }
            return bResult;
        }

        #endregion

        #region Verify Signature

        #endregion

        #endregion
    }
}
